﻿@{
    ViewBag.Title = "TransitionContractForm";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<link href="~/Content/scripts/plugins/jqgrid/jqgrid.css" rel="stylesheet" />
<script src="~/Content/scripts/plugins/jqgrid/jqgrid.min.js"></script>
<script>
    var keyValue = request('keyValue');
    var ContractId = request('ContractId');
    var MemberId = request('MemberId');
    var AuditLevel = 0;
    $(function () {
        InitControl();
        GetGrid();

    })
    //初始化数据
    function InitControl() {
        //组织机构
        $("#OrganizeId").ComboBoxTree({
            url: "../../BaseManage/Organize/GetTreeJsonById",
            height: "250px",
            description: "===选择组织机构===",
            click: function (item) {
            }
        });
        //户型
        $("#HouseTypeName").ComboBox({
            url: "../../SystemManage/DataItemDetail/GetDataItemTreeJson",
            param: { EnCode: "HouseType" },
            id: "value",
            text: "text",
            description: "==请选择==",
        });
        if (!!keyValue) {
            $.SetForm({
                url: "../../QualificationManage/Contract/GetInfo",
                param: { keyValue: keyValue, ContractId: ContractId },
                success: function (data) {
                    AuditLevel = data.CBBInfo.CurrentAuditLevel;
                    $("#Info").SetWebControls(data.ContractDWelling);
                    $(".MemberInfo").SetWebControls(data.MemberInfo);
                    $("#PStruct").SetWebControls(data.PStruct);
                    $("#OrganizeId").ComboBoxTreeSetValue(data.PStruct.OrganizeId);
                    $("#HouseTypeName").ComboBoxTreeSetValue(data.Room.HouseTypeName);
                    $("#Area").val(data.Room.Area);
                    $("#PStruct").SetWebControls(data.PStruct);
                    $("#EndDate").val(formatDate(data.ContractDWelling.EndDate, 'yyyy-MM-dd'));
                    $("#BeginDate").val(formatDate(data.ContractDWelling.BeginDate, 'yyyy-MM-dd'));
                    var RenewBeginDate = new Date(data.ContractDWelling.EndDate);
                    RenewBeginDate.setDate(RenewBeginDate.getDate() + 1);
                    $("#RenewBeginDate").val(formatDate(RenewBeginDate, 'yyyy-MM-dd'));
                    $("#NowMonthlyRent").val(data.Room.MonthlyRent);
                    $("#CreateUserName").val(data.CBBInfo.CreateUserName);
                    $("#Description").val(data.CBBInfo.Description);
                    $("#MonthlyRent").val(data.PStructInfo.MonthlyRent);
                    $("#RCode").val(data.ContractDWelling.Code);
                    $("#Code").val(data.CBBInfo.Code);
                    $("#RoomCode").val(data.PStruct.Code);
                    $("#RentYear").val(data.ContractDWelling.RentYear);
                    $("#State").text(data.CBBInfo.State == 0 ? "未提交" : data.CBBInfo.State == 1 ? "审批中" : data.CBBInfo.State == 2 ? "审批通过" : "审批未通过");
                    if (data.CBBInfo.State == 1) GetIsShow("过渡合同");
                    if (data.CBBInfo.CurrentAuditLevel >= 3) {
                        $("#CTime").show();
                        if (!!data.CBBInfo.TransitionBeginDate) {
                            $("#RenewBeginDate").val(formatDate(data.CBBInfo.TransitionBeginDate, 'yyyy-MM-dd'));
                        }
                        if (!!data.CBBInfo.TransitionEndDate) {
                            $("#EndRenewDate").val(formatDate(data.CBBInfo.TransitionEndDate, 'yyyy-MM-dd'));
                        }
                        GetDateTimes();
                    }
                   
                    if (data.CBBInfo.CurrentAuditLevel > 3) { 
                        $("#RenewBeginDate").attr("disabled", "disabled");
                        $("#EndRenewDate").attr("disabled", "disabled");
                    }

                }
            })
        }
    }
    //获取数据
    function GetTimes(sdate, edate) {
        var STime = new Date(sdate);
        var ETime = new Date(edate);
        if (ETime < STime) {
            dialogMsg("开始时间不能大于结束时间！", 0);
            return "";
        }
        var y = ETime.getFullYear() - STime.getFullYear();
        var m = ETime.getMonth() - STime.getMonth();
        var d = ETime.getDate() - STime.getDate() + 1;
        $("#RenewYear").val(Math.floor((ETime.getTime() - STime.getTime()) / 86400000) + 1);
        if (y >= 1) {
            dialogMsg("过渡期限不能大于六个月！", 0);
            return "";
        }
        if (m > 6) {
            dialogMsg("过渡期限不能大于六个月！", 0);
            return "";
        }
        if (y == 0 && m != 0) {
            return m + "个月" + d + "天";
        }
        if (y == 0 && m == 0) {
            return d + "天";
        }
    }
    //计算时间戳
    function GetDateTimes() {
        var BeginDate = $("#RenewBeginDate").val();
        var EndDate = $("#EndRenewDate").val();
        if (BeginDate != "" && EndDate != "") {
            $("#ShowRenewYear").val(GetTimes(BeginDate, EndDate));
        }
    }
    //加载审核记录
    function GetGrid() {
        var $gridTable = $("#gridTable");
        $gridTable.jqGrid({
            url: "../../QualificationManage/Contract/GetAuditList?BussinessId=" + keyValue,
            datatype: "json",
            height: 135,
            autowidth: true,
            unwritten: false,
            colModel: [
                    { label: 'ID', name: 'Id', hidden: true },
                    {
                        label: "审批结果", name: "AuditState", width: 100, align: "center", sortable: true,
                        formatter: function (cellvalue, options, rowObject) {
                            switch (cellvalue) {
                                case 2: return "不通过";
                                case 1: return "通过";
                                default: return "";
                            }
                        }
                    },
                    { label: '审批人', name: 'AuditMan', width: 100, align: "center", sortable: false },
                    { label: "审批时间", name: "AuditTime", width: 200, align: "center", sortable: false },
                    { label: "审批意见", name: "AuditMemo", width: 100, align: "left", sortable: false },
            ],
            rowNum: "1000",
            rownumbers: true,
            shrinkToFit: false,
            gridview: true,
            hidegrid: false,
            gridComplete: function () {

            }
        });
    }

    function btn_Audit(AuditState) {
        if (!$('#form1').Validform()) {
            return false;
        }
        var queryJson = $("#Audit").GetWebControls(keyValue);
        queryJson["AuditState"] = AuditState;
        queryJson["Level"] = AuditLevel;
        queryJson["BussinessId"] = keyValue;
        queryJson["ContractId"] = ContractId;
        queryJson["AuditType"] = 0;
        queryJson["AuditMemo"] = $("#AuditMemo").val();
        queryJson["EndRenewDate"] = $("#EndRenewDate").val();
        queryJson["RenewYear"] = $("#RenewYear").val();
        queryJson["BeginRenewDate"] = $("#RenewBeginDate").val();
        if (AuditState == 2 && $("#AuditMemo").val() == "") {
            dialogMsg("不同意请说明原因！", 0);
            return false;
        }
        $.ConfirmAjax({
            msg: "注：您确认要进行此操作吗？",
            url: "../../QualificationManage/Contract/AuditTransitionForm",
            param: { queryJson: JSON.stringify(queryJson) },
            loading: "正在保存数据...",
            success: function () {

                $.currentIframe().$("#gridTable").resetSelection();
                $.currentIframe().$("#gridTable").trigger("reloadGrid");
                dialogClose();
            }
        });

    }

    function GetIsShow(TypeName) {
        if (AuditLevel == 0) AuditLevel = 1;
        $.SetForm({
            url: "../../SystemManage/Setting/GetIsShow",
            param: { TypeName: TypeName, Level: AuditLevel },
            dataType: "text",
            success: function (data) {
                var Audit = JSON.parse(data);
                if (Audit.OK) {
                    $("#AuditState").show();
                    NowLevel = Audit.NowLevel;
                }
                else
                    $("#AuditState").hide();
            }
        });
    }
</script>
<style>
    #Info, #Info td {
        border: 1px solid #ccc;
    }

    .cs-1 {
        background-color: #DDF0ED;
    }

    .cs-2 {
        border: none;
    }
</style>
<div>
    <table class="form" id="CBB" style="margin-top:5px; width:90%; margin-left:5%;">
        <tr>
            <td class="formTitle">单据状态:</td>
            <td class="formValue"><span id="State" style="color:red;font-size:20px;"></span></td>
            <td class="formTitle">申请单编号:</td>
            <td class="formValue"><input  class="form-control" disabled placeholder="系统自动编号" id="Code" readonly type="text" /></td>
            <td class="formTitle">申请日期:</td>
            <td class="formValue"><input id="Date" type="text" disabled class="form-control input-wdatepicker BaseClass " readonly placeholder="请输入申请日期" value="@LeSoft.Util.Time.GetToday("yyyy-MM-dd")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd ' })" /></td>
        </tr>
    </table>

    <table class="form" id="Info" style="margin-top: 20px; width: 90%; margin-left: 5%;">
        <tr>
            <td class="formTitle cs-1">所属机构</td>
            <td class="formValue"><div id="OrganizeId" type="selectTree" readonly class="ui-select cs-2 "></div></td>
            <td class="formTitle cs-1">合同编号</td>
            <td class="formValue"><input class="form-control cs-2" id="RCode" readonly type="text" /></td>
            <td class="formTitle cs-1">签约日期</td>
            <td class="formValue"><input id="SignDate" type="text" disabled class="form-control input-wdatepicker cs-2 " disabled value="@LeSoft.Util.Time.GetToday("yyyy-MM-dd")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd ' })" /></td>
        </tr>
        <tr id="PStruct">
            <td class="formTitle cs-1">租赁房屋</td>
            <td class="formValue" colspan="3"><input class="form-control cs-2" id="AllName" readonly type="text" /></td>
            <td class="formTitle cs-1">房屋编号</td>
            <td class="formValue"><input class="form-control cs-2" id="RoomCode" readonly type="text" /></td>
        </tr>
        <tr id="Room">
            <td class="formTitle cs-1">建筑面积</td>
            <td class="formValue"><input class="form-control" id="Area" readonly type="text" /></td>
            <td class="formTitle cs-1">户型</td>
            <td class="formValue"><div id="HouseTypeName" class="ui-select  cs-2" readonly type="selectTree"></div></td>
            <td class="formTitle cs-1">每月租金</td>
            <td class="formValue"><input class="form-control cs-2" id="MonthlyRent" readonly type="text" /></td>
        </tr>
        <tr class="MemberInfo">
            <td class="formTitle cs-1">承租人</td>
            <td class="formValue"><input class="form-control cs-2" id="Name" readonly type="text" /></td>
            <td class="formTitle cs-1">身份证号</td>
            <td class="formValue"><input class="form-control cs-2" id="IDCard" readonly type="text" /></td>
            <td class="formTitle cs-1">联系电话</td>
            <td class="formValue"><input class="form-control cs-2" id="Phone" readonly type="text" /></td>
        </tr>
        <tr class="MemberInfo">
            <td class="formTitle cs-1">所属单位</td>
            <td class="formValue"><input class="form-control cs-2" id="CompanyName" readonly type="text" /></td>
            <td class="formTitle cs-1">单位地址</td>
            <td class="formValue"><input class="form-control cs-2" id="CompanyAddress" readonly type="text" /></td>
            <td class="formTitle cs-1">单位电话</td>
            <td class="formValue"><input class="form-control cs-2" id="CompanyPhone" readonly type="text" /></td>
        </tr>
        <tr>
            <td class="formTitle cs-1">租赁开始时间</td>
            <td class="formValue"><input id="BeginDate" type="text" disabled readonly class="form-control input-wdatepicker cs-2 " readonly placeholder="请输入申请日期" value="@LeSoft.Util.Time.GetToday("yyyy-MM-dd ")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd ' })" /></td>
            <td class="formTitle cs-1">租赁结束时间</td>
            <td class="formValue"><input id="EndDate" type="text" disabled readonly class="form-control input-wdatepicker cs-2 " readonly placeholder="请输入申请日期" value="@LeSoft.Util.Time.GetToday("yyyy-MM-dd ")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd ' })" /></td>
            <td class="formTitle cs-1">租赁年限</td>
            <td class="formValue">
                <input class="form-control cs-2" id="RentYear" readonly type="text" />
            </td>
        </tr>

    </table>
    <table class="form" id="Info1" style="margin-top: 20px; width: 90%; margin-left: 5%;">
        <tr hidden id="CTime">
            <td class="formTitle">租赁开始日期</td>
            <td class="formValue"><input id="RenewBeginDate" type="text" isvalid="yes" checkexpession="NotNull" class="form-control input-wdatepicker BaseClass " onblur="GetDateTimes();" placeholder="请输入开始日期" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd ', minDate: '#F{$dp.$D(\'EndDate\')}'}) " /></td>
            <td class="formTitle">租赁终止日期</td>
            <td class="formValue"><input id="EndRenewDate" type="text" isvalid="yes" checkexpession="NotNull" class="form-control input-wdatepicker BaseClass " onblur="GetDateTimes();" placeholder="请输入结束日期" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd ', minDate: '#F{$dp.$D(\'EndDate\')}'}) " /></td>
            <td class="formTitle">过渡期限</td>
            <td class="formValue">
                <input id="RenewYear" type="hidden" />
                <input id="ShowRenewYear" readonly disabled type="text" isvalid="yes" checkexpession="NotNull" class="form-control " />
            </td>
        </tr>
        <tr>
            <td class="formTitle">最新租金</td>
            <td class="formValue"><input id="NowMonthlyRent" readonly disabled class="form-control " isvalid="yes" checkexpession="NotNull" type="text" /></td>
            <td class="formTitle">申请人</td>
            <td class="formValue"><input id="CreateUserName" readonly disabled class="form-control" isvalid="yes" checkexpession="IsScript" type="text" /></td>
        </tr>
        <tr>
            <td class="formTitle">附加说明</td>
            <td class="formValue" colspan="5"><textarea id="Description" readonly disabled class="form-control BaseClass" style="height: 80px;" isvalid="yes" checkexpession="IsScript"></textarea></td>
        </tr>
        <tr style="text-align:center;"></tr>
    </table>
    <table class="form" id="Audit" style="margin-top: 5px; width: 90%; margin-left: 5%;">
        <tr>
            <td colspan="6">
                <table id="gridTable"></table>
            </td>
        </tr>
        <tr id="AuditState" hidden>
            <td class="formTitle">审批意见：</td>
            <td class="formValue" colspan="3"><input id="AuditMemo" class="form-control" type="text" isvalid="yes" checkexpession="IsScript" /></td>
            <td class="formValue" colspan="2">
                <a id="lr-addRule" class="btn btn-success btn-xs" style="width:150px;height:28px;" onclick="btn_Audit(1)">同意</a>
                <a id="lr-deleteRule" class="btn btn-danger btn-xs" style="width: 150px; height: 28px;" onclick="btn_Audit(2)">不同意</a>
            </td>
        </tr>
    </table>
</div>


